###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

package AIR2::Searcher::Responses::Engine;
use strict;
use warnings;
use base qw( AIR2::Searcher::Engine );
use Carp;
use Data::Dump qw( dump );

my @summary_fields = qw(

);

# TODO
my %multival_fields = map { $_ => $_ } qw(
    qa
);

my %collapse_dupe_fields = (

);

sub process_result {
    my ( $self, %args ) = @_;
    my $result       = $args{result};
    my $hiliter      = $args{hiliter};
    my $XMLer        = $args{XMLer};
    my $snipper      = $args{snipper};
    my $fields       = $args{fields};
    my $apply_hilite = $args{apply_hilite};

    my %res = (
        score => $result->score,
        uri   => $result->uri,
        mtime => $result->get_property('srs_upd_dtim'),
    );

    $self->_fetch_prop_values( $result, \%res, $fields, \%multival_fields,
        \%collapse_dupe_fields, $XMLer );

    # hand-craft some values
    $res{uri}   = $res{srs_id};
    $res{title} = $res{src_name};
    $res{mtime} =~ s/\ .+$//;    # strip time

    # build the summary
    $self->_build_summary( $result, $snipper, \%res );

    # finally, apply hiliting
    if ($apply_hilite) {
        for my $f (
            qw(
            summary
            title
            )
            )
        {
            $res{$f} = $hiliter->light( $res{$f} );
        }
    }

    return \%res;
}

sub _build_summary {
    my ( $self, $swish_result, $snipper, $result ) = @_;
    my $query = $snipper->query;

    my @summary;

    for my $qa ( @{ $result->{qa} } ) {
        my ( $org_ids, $inq_uuid, $ques_seq, $date, $response )
            = (
            $qa =~ m/^(.+?):(\w+):(\d+):(\d+-\d+-\d+ \d+:\d+:\d+):(.*)$/s );
        if ( !defined($response) or !length($response) ) {
            next;
        }

        # IMPORTANT turn on utf8 flag
        $response = Search::Tools::UTF8::to_utf8($response);
        if ( $query->matches_text($response) ) {
            push @summary, $response;
            last;    # TODO match more?
        }
    }

FIELD: for my $f (@summary_fields) {

        # already fetched
        if ( exists $result->{$f} ) {
            if ( ref $result->{$f} ) {
                for my $v ( @{ $result->{$f} } ) {
                    if ( $query->matches_text($v) ) {
                        push @summary, $v;
                        next FIELD;
                    }
                }
            }
            elsif ( $query->matches_text( $result->{$f} ) ) {
                push @summary, $result->{$f};
                next FIELD;
            }
        }

        # need to fetch
        else {
            my $prop;
            eval { $prop = $swish_result->get_property($f); };
            if ($prop) {
                for my $v ( split( m/\003/, $prop ) ) {
                    if ( $query->matches_text($v) ) {
                        push @summary, $v;
                        next FIELD;
                    }
                }
            }
        }
    }

    return $self->_cleanup_summary( $result, @summary );
}

1;
